/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/8/12
 * Time: 21:21
 */
public class Solution {
//    public boolean isHappy(int n) {
//        int slow = n;
//        int fast = n;
//        while (true){
//            String s1 = Integer.toString(slow);
//            int total_s = 0;
//            for (int i = 0; i < s1.length(); i++) {
//                total_s += (s1.charAt(i) - '0') * (s1.charAt(i) - '0');
//            }
//            slow = total_s;
//            String s2 = Integer.toString(fast);
//            int total_f = 0;
//            for (int i = 0; i < s2.length(); i++) {
//                total_f += (s2.charAt(i) - '0') * (s2.charAt(i) - '0');
//            }
//            fast = total_f;
//            total_f = 0;
//            String s3 = Integer.toString(fast);
//            for (int i = 0; i < s3.length(); i++) {
//                total_f += (s3.charAt(i) - '0') * (s3.charAt(i) - '0');
//            }
//            fast = total_f;
//            if((slow == fast) && slow == 1){
//                return true;
//            }
//            if(slow == fast){
//                return false;
//            }
//        }
//    }
    public int bitSum(int n){
        int sum = 0;
        while(n != 0){
            int temp = n % 10;
            sum +=  temp * temp;
            n /= 10;
        }
        return sum;
    }
    public boolean isHappy(int n){
        int slow = n;
        int fast = bitSum(n);
        while(fast != slow){
            slow = bitSum(slow);
            fast = bitSum(bitSum(fast));
        }
        return slow == 1;
    }
    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.isHappy(2));
    }
}